package com.example.springboot2.oa.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot2.base.web.BaseController;
import com.example.springboot2.base.web.BizOrderState;
import com.example.springboot2.oa.entity.TCar;
import com.example.springboot2.oa.entity.TCarborrow;
import com.example.springboot2.oa.service.ITCarService;
import com.example.springboot2.oa.service.ITCarborrowService;
import com.example.springboot2.tools.MakeResponse;
import com.example.springboot2.tools.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.function.Consumer;

@CrossOrigin
@RestController
@RequestMapping("/oa/t-carborrow")
public class TCarborrowController extends BaseController {
    @Autowired
    private ITCarService carService;
    @Autowired
    private ITCarborrowService carborrowService;

    private boolean lockCar(TCarborrow carborrow) {
        TCar car = new TCar();
        car.setCarState(1);
        car.setCarId(carborrow.getCarId());
        QueryWrapper<TCar> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("car_id", car.getCarId());
        return carService.update(car, wrapper2);
    }

    private boolean unlockCar(TCarborrow carborrow) {
        TCar car = new TCar();
        car.setCarState(0);
        car.setCarId(carborrow.getCarId());
        QueryWrapper<TCar> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("car_id", car.getCarId());
        return carService.update(car, wrapper2);
    }

    /**
     * 用户列表
     * @param request
     * @return
     */
    @PostMapping("listAll")
    public ResponseMessage listAll(HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        QueryWrapper<TCarborrow> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("create_time");
        List<TCarborrow> data = carborrowService.list(wrapper);
        return MakeResponse.success("200", "成功", data);
    }

    /**
     * 添加车辆
     * @param carborrow
     * @param request
     * @return
     */
    @PostMapping("add")
    public ResponseMessage add(@RequestBody TCarborrow carborrow, HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        carborrow.setCreateTime(new Date());
        carborrow.setId(String.valueOf(UUID.randomUUID()));
        if (carborrow.getBizorderState() > BizOrderState.BIZ_ORDER_STATE_BORROW) {
            return MakeResponse.success("500", "状态错误，只能选择开始/出借");
        }

        if (carborrow.getBizorderState() == BizOrderState.BIZ_ORDER_STATE_BORROW) {
            carborrow.setBorrowTime(new Date());
            lockCar(carborrow);
        }

        if (!carborrowService.save(carborrow)) {
            return MakeResponse.success("500", "新增失败");
        }

        return MakeResponse.success("200", "新增成功");
    }

    /**
     * 批量删除用户
     * @param carborrowList
     * @param request
     * @return
     */
    @PostMapping("delete")
    public ResponseMessage delete(@RequestBody List<TCarborrow> carborrowList, HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        List<String> carborrowIdList = new ArrayList<>();
        carborrowList.forEach(new Consumer<TCarborrow>() {
            @Override
            public void accept(TCarborrow carborrow) {
                carborrowIdList.add(carborrow.getId());
            }
        });

        QueryWrapper<TCarborrow> wrapper = new QueryWrapper<>();
        wrapper.in("id", carborrowIdList);
        if (!carborrowService.remove(wrapper)) {
            return MakeResponse.success("500", "批量删除失败");
        }

        return MakeResponse.success("200", "批量删除成功");
    }

    /**
     * 更新车辆信息
     * @param carborrow
     * @param request
     * @return
     */
    @PostMapping("modify")
    public ResponseMessage modify(@RequestBody TCarborrow carborrow, HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        QueryWrapper<TCarborrow> wrapper = new QueryWrapper<>();
        wrapper.eq("id", carborrow.getId());
        TCarborrow carborrow2 = carborrowService.getOne(wrapper);
        if (carborrow2 == null) {
            return MakeResponse.success("500", "此记录项目不存在");
        }

        if(carborrow.getBizorderState() <= carborrow2.getBizorderState()) {
            return MakeResponse.success("500", "订单状态不能往回滚");
        }

        if(carborrow.getBizorderState() > carborrow2.getBizorderState()+1) {
            return MakeResponse.success("500", "订单状态往前跳太多");
        }

        switch (carborrow.getBizorderState()) {
            case BizOrderState.BIZ_ORDER_STATE_BEGIN:{
                break;
            }
            case BizOrderState.BIZ_ORDER_STATE_BORROW:{
                carborrow.setBorrowTime(new Date());
                lockCar(carborrow);
                break;
            }
            case BizOrderState.BIZ_ORDER_STATE_CARBACK:{
                carborrow.setCarbackTime(new Date());
                unlockCar(carborrow);
                break;
            }
            case BizOrderState.BIZ_ORDER_STATE_END:{
                carborrow.setEndTime(new Date());
                break;
            }
        }

        carborrow.setUpdateTime(new Date());
        if (!carborrowService.updateById(carborrow)) {
            return MakeResponse.success("500", "修改失败");
        }

        return MakeResponse.success("200", "修改成功");
    }
}
